#include "src/View/filetreeview.h"


FileTreeView::FileTreeView(QWidget* parent /*= nullptr*/)
    : QTreeView(parent)
{
    iniFileTreeView();
    createMenus();
    createActions();

}

FileTreeView::~FileTreeView()
{
    //destroyed();

}

void FileTreeView::iniFileTreeView()
{
    fileModel=new QFileSystemModel;
    fileModel->setRootPath("/");
    fileModel->setOption(QFileSystemModel::DontUseCustomDirectoryIcons);
    this->setModel(fileModel);
    this->setAnimated(true);
    this->setIndentation(20);
    this->setSortingEnabled(true);
    const QSize availableSize=this->screen()->availableGeometry().size();
    this->resize(availableSize/2);
    this->setColumnWidth(0,this->width()/3);
    this->setEditTriggers(QTreeView::NoEditTriggers);
    this->setSelectionBehavior(QTreeView::SelectRows);
    //this->setAlternatingRowColors(true);
    this->setFocusPolicy(Qt::NoFocus);
    this->setContextMenuPolicy(Qt::CustomContextMenu);

    /*
    this->header()->setHighlightSections(true);
    this->header()->setCursor(Qt::OpenHandCursor);
    this->header()->setDefaultAlignment(Qt::AlignCenter);
    this->header()->setDefaultSectionSize(100);
    this->header()->setStretchLastSection(true);
    this->header()->setSortIndicator(0,Qt::AscendingOrder);
    */

    //connect(this,&QTreeView::customContextMenuRequested,this,&FileTreeView::on_popRightMenu);
}

void FileTreeView::createMenus()
{
    popMenuOnFolder=new QMenu(this);

}

void FileTreeView::createActions()
{
    actAddFolder=new QAction(QObject::tr("Add Folder"));
    popMenuOnFolder->addAction(actAddFolder);
    connect(actAddFolder,&QAction::triggered,this,&FileTreeView::on_actAddFolder_triggered);

    actRemoveFolder=new QAction(QObject::tr("Remove Folder"));
    popMenuOnFolder->addAction(actRemoveFolder);
    connect(actRemoveFolder,&QAction::triggered,this,&FileTreeView::on_actRemoveFolder_triggered);
}
// rightButton menu method 1
void FileTreeView::mousePressEvent(QMouseEvent *event)
{
    QTreeView::mousePressEvent(event);
    QModelIndex curIndex=currentIndex();
    QString curFilePath=fileModel->filePath(curIndex);
    if(event->buttons()==Qt::RightButton)
    {
        qDebug()<<"triggered mouse rightbutton clicked"<<endl;
        qDebug()<<"file type:"<<fileModel->type(curIndex)<<endl;

        if((!curFilePath.isEmpty())&&fileModel->isDir(curIndex))
        {
            qDebug()<<"current file name:"<<fileModel->fileName(curIndex)<<endl;
            qDebug()<<"current file path:"<<fileModel->filePath(curIndex)<<endl;
            popMenuOnFolder->exec(QCursor::pos());
        }
        else
        {

            qDebug()<<"this not is dir"<<endl;
        }
    }

}

void FileTreeView::contextMenuEvent(QContextMenuEvent */*event*/)
{
    QModelIndex curIndex=this->currentIndex();
    qDebug()<<"triggered contextMenuEvent"<<endl;
    if(fileModel->isDir(curIndex))
    {
        popMenuOnFolder->exec(QCursor::pos());
    }
    else
    {
        qDebug()<<"this not is dir"<<endl;
    }
}

// rightButton menu method 2
void FileTreeView::on_popRightMenu(const QPoint &pos)
{
    QModelIndex curIndex=this->indexAt(pos);
    if(fileModel->isDir(curIndex))
    {
        qDebug()<<"current file name:"<<fileModel->fileName(curIndex)<<endl;
        qDebug()<<"current file path:"<<fileModel->filePath(curIndex)<<endl;
        popMenuOnFolder->exec(QCursor::pos());
    }
    else
    {

        qDebug()<<"this not is dir"<<endl;
    }
}

void FileTreeView::on_actAddFolder_triggered()
{
    QModelIndex curIndex=currentIndex();

    QString dlgTitle=QObject::tr("New Folder");
    QString txtLabel=QObject::tr("Input Folder Name");
    QString defaultInput=QObject::tr("new folder");
    QLineEdit::EchoMode echoMode=QLineEdit::Normal;
    bool ok=false;
    QString newFolderName=QInputDialog::getText(this,dlgTitle,txtLabel,echoMode,defaultInput,&ok);
    if(ok&&!newFolderName.isEmpty())
    {
        fileModel->mkdir(curIndex,newFolderName);
    }
}

void FileTreeView::on_actRemoveFolder_triggered()
{
    QModelIndex curIndex=currentIndex();

    QString dlgTitle=QObject::tr("Remove Folder");
    QString strInfo=QObject::tr("Are you sure to remove this folder?");
    QMessageBox::StandardButton defaultButton=QMessageBox::NoButton;
    QMessageBox::StandardButton resultButton;
    resultButton=QMessageBox::question(this,dlgTitle,strInfo,QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,defaultButton);
    if(resultButton==QMessageBox::Yes)
    {
        fileModel->rmdir(curIndex);
    }
    else
    {

    }
}
